package com.jsb.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.annotation.SystemControllerLog;
import com.jsb.service.InvoiceService;
import com.jsb.service.PurchaseOrderService;
import com.jsb.service.UserDealRecordService;
import com.jsb.service.UserService;
import com.jsb.dto.UserDealRecordComplex;
import com.jsb.model.Invoice;
import com.jsb.model.InvoiceExample;
import com.jsb.model.User;
import com.jsb.model.UserDealRecord;
import com.jsb.model.UserDealRecordExample;
import com.jsb.model.InvoiceExample.Criteria;
import com.jsb.model.PurchaseOrder;
import com.jsb.model.PurchaseOrderExample;

@Controller
public class PurchaseOrderController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/purchaseOrder/list.do";
	
	@Resource
	private PurchaseOrderService purchaseOrderService;
	@Resource
	private InvoiceService invoiceService;
	@Resource
	private UserService userService;
	@Resource
	private UserDealRecordService userDealRecordService;
	
	@RequestMapping("/purchaseOrder/list")
	public String list(
			Model model,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		PurchaseOrderExample purchaseOrderExample = new PurchaseOrderExample();
		int totalCount = purchaseOrderService.countPurchaseOrder(purchaseOrderExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		purchaseOrderExample.setPageIndex((pageNum - 1) * pageSize);
		purchaseOrderExample.setPageSize(pageSize);
		List<PurchaseOrder> purchaseOrders = purchaseOrderService.getPagePurchaseOrder(purchaseOrderExample);
		//将invoice对象封装进purchaseorders中
		for (PurchaseOrder purchaseOrder : purchaseOrders) {
			String phoneNum1 = purchaseOrder.getPhoneNum();
			InvoiceExample invoiceExample = new InvoiceExample();
			Criteria createCriteria = invoiceExample.createCriteria();
			createCriteria.andUserPhoneNumEqualTo(phoneNum1);
			//根据用户电话号码，查询出对象的invoice，结果也只能有一条
			List<Invoice> invoices = invoiceService.selectByExample(invoiceExample);
			if(invoices !=null && invoices.size()>0){
				purchaseOrder.setInvoice(invoices.get(0));
			}
		}
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("purchaseOrders", purchaseOrders);
		return "/PurchaseOrder/list";
	}
	@RequestMapping("/purchaseOrder/search")
	public String search(
			Model model,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			String phoneNum,Integer invoiceStatus) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		PurchaseOrderExample purchaseOrderExample = new PurchaseOrderExample();
		List<User> userByLikePhone = userService.getUserByLikePhone(phoneNum);
		for (User user : userByLikePhone) {
			String userid = user.getPhoneNumber();
			//添加搜索条件
			com.jsb.model.PurchaseOrderExample.Criteria purchaseOrderCriteria = purchaseOrderExample.createCriteria();
			if(phoneNum !=null && !"".equals(userid)){
				purchaseOrderCriteria.andPhoneNumLike("%"+userid+"%");
			}
			//如果状态小于等于1时，就查询特定的值
			if(invoiceStatus !=null ){
				if(invoiceStatus <=1){
					if(invoiceStatus == 0){
						purchaseOrderCriteria.andPaymentStatusEqualTo("0");
					}
					purchaseOrderCriteria.andInvoiceStatusEqualTo(invoiceStatus);
				}else{
					//如果状态大于1时，就查询状态大于1的值
					purchaseOrderCriteria.andInvoiceStatusGreaterThanOrEqualTo(2);
				}
			}
			purchaseOrderExample.or(purchaseOrderCriteria);
		}
		int totalCount = purchaseOrderService.countPurchaseOrder(purchaseOrderExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		purchaseOrderExample.setPageIndex((pageNum - 1) * pageSize);
		purchaseOrderExample.setPageSize(pageSize);
		List<PurchaseOrder> purchaseOrders = purchaseOrderService.getPagePurchaseOrder(purchaseOrderExample);
		//将invoice对象封装进purchaseorders中
		for (PurchaseOrder purchaseOrder : purchaseOrders) {
			String phoneNum1 = purchaseOrder.getPhoneNum();
			InvoiceExample invoiceExample = new InvoiceExample();
			Criteria createCriteria = invoiceExample.createCriteria();
			createCriteria.andUserPhoneNumEqualTo(phoneNum1);
			//根据用户电话号码，查询出对象的invoice，结果也只能有一条
			List<Invoice> invoices = invoiceService.selectByExample(invoiceExample);
			if(invoices !=null && invoices.size()>0){
				purchaseOrder.setInvoice(invoices.get(0));
			}
		}
		model.addAttribute("phoneNum", phoneNum);
		model.addAttribute("invoiceStatus", invoiceStatus);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("purchaseOrders", purchaseOrders);
		return "/PurchaseOrder/list";
	}
	
	@RequestMapping("/purchaseOrder/toAdd")
	public String toAdd(Model model) {
		return "/PurchaseOrder/add";
	}

	@RequestMapping("/purchaseOrder/add")
	public String add(Model model, @ModelAttribute("purchaseOrder") PurchaseOrder purchaseOrder) {
		purchaseOrderService.addPurchaseOrder(purchaseOrder);
		return redirect;
	}

	@RequestMapping("/purchaseOrder/del")
	public String del(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		purchaseOrderService.delPurchaseOrderByUuid(uuid);
		return redirect;
	}

	@RequestMapping("/purchaseOrder/toEdit")
	public String toEdit(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		PurchaseOrder purchaseOrder = purchaseOrderService.getPurchaseOrderByUuid(uuid);
		String phoneNum = purchaseOrder.getPhoneNum();
		InvoiceExample invoiceExample = new InvoiceExample();
		Criteria createCriteria = invoiceExample.createCriteria();
		createCriteria.andUserPhoneNumEqualTo(phoneNum);
		//根据用户电话号码，查询出对象的invoice，结果也只能有一条
		List<Invoice> invoices = invoiceService.selectByExample(invoiceExample);
		if(invoices !=null && invoices.size()>0){
			purchaseOrder.setInvoice(invoices.get(0));
		}
		model.addAttribute("purchaseOrder", purchaseOrder);
		return "/PurchaseOrder/edit";
	}

	@RequestMapping("/purchaseOrder/edit")
	@SystemControllerLog(description = "编辑发票信息")
	public String edit(Model model, @ModelAttribute("purchaseOrder") PurchaseOrder purchaseOrder) {
		//更新发票
//		purchaseOrderService.updatePurchaseOrder(purchaseOrder);
		Invoice invoice = purchaseOrder.getInvoice();
		invoiceService.updateInvoice(invoice);
		return redirect;
	}

	@RequestMapping("/purchaseOrder/view")
	public String view(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		PurchaseOrder purchaseOrder = purchaseOrderService.getPurchaseOrderByUuid(uuid);
		String phoneNum = purchaseOrder.getPhoneNum();
		InvoiceExample invoiceExample = new InvoiceExample();
		Criteria createCriteria = invoiceExample.createCriteria();
		createCriteria.andUserPhoneNumEqualTo(phoneNum);
		//根据用户电话号码，查询出对象的invoice，结果也只能有一条
		List<Invoice> invoices = invoiceService.selectByExample(invoiceExample);
		if(invoices !=null && invoices.size()>0){
			purchaseOrder.setInvoice(invoices.get(0));
		}
		model.addAttribute("purchaseOrder", purchaseOrder);
		return "/PurchaseOrder/view";
	}
	@RequestMapping("/purchaseOrder/viewDetail")
	public String viewDetail(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		PurchaseOrder purchaseOrder = purchaseOrderService.getPurchaseOrderByUuid(uuid);
		String phoneNum = purchaseOrder.getPhoneNum();
		InvoiceExample invoiceExample = new InvoiceExample();
		Criteria createCriteria = invoiceExample.createCriteria();
		createCriteria.andUserPhoneNumEqualTo(phoneNum);
		//根据用户电话号码，查询出对象的invoice，结果也只能有一条
		List<Invoice> invoices = invoiceService.selectByExample(invoiceExample);
		if(invoices !=null && invoices.size()>0){
			purchaseOrder.setInvoice(invoices.get(0));
		}
		model.addAttribute("purchaseOrder", purchaseOrder);
		return "/PurchaseOrder/viewDetail";
	}
	//开票的方法
	@RequestMapping("/purchaseOrder/open")
	@SystemControllerLog(description = "开发票")
	public String open(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		PurchaseOrder purchaseOrder = purchaseOrderService.getPurchaseOrderByUuid(uuid);
		Integer invoiceStatus = purchaseOrder.getInvoiceStatus();
		//对开票次数加一
		if(invoiceStatus==-1 || invoiceStatus==0){
			invoiceStatus=1;
		}else{
			invoiceStatus++;
		}
		purchaseOrder.setInvoiceStatus(invoiceStatus);
		purchaseOrderService.updatePurchaseOrder(purchaseOrder);
		return redirect;
	}
	//购买会员卡的资金详情
	@RequestMapping("/purchaseOrder/moneyDetail")
	public String moneyDetail(Model model,
			@RequestParam(value = "uuid", required = true) String uuid,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		
		UserDealRecordExample dealRecordExample= new UserDealRecordExample();
		com.jsb.model.UserDealRecordExample.Criteria criteria = dealRecordExample.createCriteria();
		criteria.andBusinessNoEqualTo(uuid);
		int totalCount = userDealRecordService.countUserDealRecord(dealRecordExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		dealRecordExample.setPageIndex((pageNum - 1) * pageSize);
		dealRecordExample.setPageSize(pageSize);
		
		dealRecordExample.setOrderByClause("createDate desc");
		List<UserDealRecord> userDealRecords = userDealRecordService.selectByExample(dealRecordExample);
		List<UserDealRecordComplex> userDealRecordComplexs= new ArrayList<UserDealRecordComplex>();
		for (UserDealRecord userDealRecord : userDealRecords) {
			UserDealRecordComplex userDealRecordComplex= new UserDealRecordComplex();
			userDealRecordComplex.setUserDealRecord(userDealRecord);
			String userId = userDealRecord.getUserId();
			User user = userService.getUserByPhoneNumber(userId);
			userDealRecordComplex.setUser(user);
			userDealRecordComplexs.add(userDealRecordComplex);
		}
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("userDealRecordComplexs", userDealRecordComplexs);
		return "/PurchaseOrder/moneyDetail";
	}
}
